import axios from "axios";
import { Message } from "element-ui";
import store from "@/store";

const service = axios.create({
  baseURL: "https://slwl-api.itheima.net/",
  // baseURL: process.env.VUE_APP_BASE_API,
  timeout: 10000,
});

//      请求拦截器
service.interceptors.request.use(
  (config) => {
    // console.log(store.getters.token)
    // const tokens = JSON.parse(store.getters.token).token
    // console.log(tokens)
    if (store.getters.token) {
      config.headers.Authorization = `${store.getters.token}`;
      // config.headers.Authorization = tokens
    }
    // config.headers['Content-Type'] = 'application/json'
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

//      响应拦截器
service.interceptors.response.use(
  (response) => {
    // console.log(response.data)
    return response;
  },
  (error) => {
    Message({ type: "error", message: error.message });
    return Promise.reject(error);
  }
);

export default service;
